/*
 * protocol_packet.h
 *
 *  Created on: 06.07.2013
 *      Author: SpirTanol
 */

#ifndef PROTOCOL_PACKET_H_
#define PROTOCOL_PACKET_H_

#include "packets/data_packet.h"
#include "global.h"

class EventData
{
	int oCode;
	uint oTime;

public:
	EventData(int code, uint time) : oCode(code), oTime(time) {}
	virtual ~EventData() {}

	inline int code() const { return oCode; }
	inline uint time() const { return oTime; }

	virtual int version() const { return 0; }
};

class ProtocolPacket : public DataPacket
{
	enum Reg
	{
		R_CODE,
		R_TIME,

		R_SIZE = R_TIME + 2,
	};

	ushort oData[R_SIZE * 10];

public:
	ProtocolPacket() { init(); }
	ProtocolPacket(const ProtocolPacket *packet);
	~ProtocolPacket() {}

	inline int code(int idx) const { return int(oData[R_SIZE * idx + R_CODE]); }
	inline uint time(int idx) const { return *(uint*)(&oData[R_SIZE * idx + R_TIME]); }

	virtual EventData *event(int idx) const { return new EventData(code(idx), time(idx)); }

	inline ProtocolPacket *clone() const { return new ProtocolPacket(this); }

private:
	inline void init() { addPart(Part(oData, R_SIZE * 10, 140)); }

};

class EventDataV50 : public EventData
{
	float oMaxAmplitude;

public:
	EventDataV50(int code, uint time, float maxAmplitude) : EventData(code, time), oMaxAmplitude(maxAmplitude) {}
	~EventDataV50() {}

	inline float maxAmplitude() const { return oMaxAmplitude; }

	int version() const { return 50; } // [virtual]
};

class ProtocolPacketV50 : public ProtocolPacket
{
	ushort oData[10];
	ushort oCurrentScale;

public:
	ProtocolPacketV50() : ProtocolPacket() { addPart(Part(oData, 10, 180)); addPart(Part(&oCurrentScale, 1, 1)); }
	ProtocolPacketV50(const ProtocolPacketV50 *packet);
	~ProtocolPacketV50() {}

	inline float maxAmplitude(int idx) const { return float(oData[idx]) / float(oCurrentScale); }

	inline ProtocolPacketV50 *clone() const { return new ProtocolPacketV50(this); } // [virtual]

	EventDataV50 *event(int idx) const { return new EventDataV50(code(idx), time(idx), maxAmplitude(idx)); } // [virtual]

};

#endif /* PROTOCOL_PACKET_H_ */
